package com.wxw.BaseLibrary.component;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import com.wxw.BaseLibrary.drawable.LayoutParamTool;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.IListBoxItem;
import com.wxw.BaseLibrary.tool.L;
import com.wxw.BaseLibrary.tool.SystemTool;

public class ListBoxPicture implements IListBoxItem {
    private TextView tv_caption;
    private EditText et_memo;
    private ListBox parent;
    private PlusImageButton btn_camera;
    private PlusImageButton btn_openfile;
    private ImageView imageView;
    private String key;
    private boolean bChanged = false;
    private String caption = "";
    private String value = "";
    private String picture = "";
    private boolean enable = true;
    private int[] BackgroundColor = new int[]{Color.parseColor("#ffffff")};
    private int BorderColor = Color.parseColor("#17a0ff");
    private int BorderWidth = 0;
    private int _ibtnCameraPicID = 0;
    private int _ibtnOpenfilePicID = 0;
    private int radians = 0;
    private LinearLayout linearLayout;
    private ListBox.OnListBoxItemChangedListener listBoxItemChangedListener;
    private OnCameraButtonListener onCameraButtonListener;
    private OnOpenfileButtonListener onOpenfileButtonListener;

    public ListBoxPicture(ListBox parent) {
        this.parent = parent;
        this._ibtnCameraPicID = parent.getResources().getIdentifier("camera", "drawable", parent.getContext().getPackageName());
        this._ibtnOpenfilePicID = parent.getResources().getIdentifier("openfile", "drawable", parent.getContext().getPackageName());
    }

    public ListBoxPicture(ListBox parent, int btnCameraPicID, int btnOpenfilePicID) {
        this.parent = parent;
        this._ibtnCameraPicID = btnCameraPicID;
        this._ibtnOpenfilePicID = btnOpenfilePicID;
    }

    @Override
    public View getView() {
        if (linearLayout==null) {
            initView();
        }
        return linearLayout;
    }

    public void setCaption(String caption){
        this.caption = caption;
        if (tv_caption != null) {
            tv_caption.setText(caption);
        }
    }

    public String getCaption() {
        return caption;
    }

    public void setMemo(String value) {
        this.value = value;
        if (et_memo != null) {
            et_memo.setText(value);
        }
    }

    public String getMemo() {
        value = et_memo != null?et_memo.getText().toString():value;
        return value;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
        if (!"".equals(picture.trim()) && imageView != null) {
            try {
                imageView.setImageBitmap(BitmapFactory.decodeFile(picture));
            } catch (Exception e) {
                L.WriteExceptionLog("ListBoxPicture", "setPicture", e.getMessage());
            }
        }
    }

    @Override
    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public void setChanged(boolean b) {
        bChanged = b;
        et_memo.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{(bChanged?parent.getItemChangedBgColor():Color.WHITE)}, 0, 0, 0));

    }

    @Override
    public boolean isChanged() {
        return bChanged;
    }

    @Override
    public void setOnListBoxItemChangedListener(ListBox.OnListBoxItemChangedListener listener) {
        listBoxItemChangedListener = listener;
    }

    public void setOnCameraButtonListener(OnCameraButtonListener onCameraButtonListener) {
        this.onCameraButtonListener = onCameraButtonListener;
    }

    public void setOnOpenfileButtonListener(OnOpenfileButtonListener onOpenfileButtonListener) {
        this.onOpenfileButtonListener = onOpenfileButtonListener;
    }

    @Override
    public ListBox getParent() {
        return parent;
    }

    public void setHint(String hint){
        et_memo.setHint(hint);
    }

    public void setEnabled(boolean enable){
        this.enable = enable;
        if (et_memo!=null) {
            et_memo.setEnabled(enable);
        }
        if (btn_camera!=null) {
            btn_camera.setEnabled(enable);
        }
        if (btn_openfile!=null) {
            btn_openfile.setEnabled(enable);
        }
    }

    public boolean isEnabled(){
        return enable;
    }

    public int[] getBackgroundColor() {
        return BackgroundColor;
    }

    public void setBackgroundColor(int[] backgroundColor) {
        BackgroundColor = backgroundColor;
        refreshLayoutBackground();
    }

    public int getBorderColor() {
        return BorderColor;
    }

    public void setBorderColor(int borderColor) {
        BorderColor = borderColor;
        refreshLayoutBackground();
    }

    public int getBorderWidth() {
        return BorderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        BorderWidth = borderWidth;
        refreshLayoutBackground();
    }

    public int getRadians() {
        return radians;
    }

    public void setRadians(int radians) {
        this.radians = radians;
        refreshLayoutBackground();
    }

    private void refreshLayoutBackground(){
        if (linearLayout!=null) {
            linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));
        }
    }

    private void initView(){
        linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));
        linearLayout.setLayoutParams(LayoutParamTool.createParams(5, 10, 5, 10));

        tv_caption = new TextView(parent.getContext());
        tv_caption.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        tv_caption.setBackgroundColor(Color.WHITE);
        tv_caption.setTextColor(Color.BLACK);
        tv_caption.setText(caption);
        linearLayout.addView(tv_caption, new LinearLayout.LayoutParams(this.parent.getCaptionWidth(), LinearLayout.LayoutParams.MATCH_PARENT));

        LinearLayout right = new LinearLayout(parent.getContext());
        right.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(right, LayoutParamTool.createParams(5, 10, 5, 10));

        LinearLayout r_top = new LinearLayout(parent.getContext());
        r_top.setOrientation(LinearLayout.HORIZONTAL);
        r_top.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, 0));
        r_top.setLayoutParams(LayoutParamTool.createParams(5, 10, 5, 10));

        right.addView(r_top, LayoutParamTool.createParams(1));

        imageView = new ImageView(parent.getContext());
        r_top.addView(imageView, LayoutParamTool.createParams(5, 100, 100));


        btn_camera = new PlusImageButton(parent.getContext());
        btn_camera.setBackgroundResource(_ibtnCameraPicID);
        btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = SystemTool.getSDCardPath() + SystemTool.createRandom(false, 10) + ".jpg";
                if (onCameraButtonListener != null) {
                    onCameraButtonListener.onClick(v, s);
                }
            }
        });
        r_top.addView(btn_camera,LayoutParamTool.createParams(5, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        btn_openfile = new PlusImageButton(parent.getContext());
        btn_openfile.setBackgroundResource(_ibtnOpenfilePicID);
        btn_openfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOpenfileButtonListener != null) {
                    onOpenfileButtonListener.onClick(v);
                }
            }
        });
        r_top.addView(btn_camera,LayoutParamTool.createParams(5, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));



        et_memo = new LineEditText(parent.getContext());
        et_memo.setText(value);
        et_memo.setEnabled(enable);
        et_memo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                triggerEvent(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        editHelperClass.editText.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
//                new int[]{getResources().getColor(R.color.cc_white),getResources().getColor(R.color.cc_white)}, 0, 0, 0));
//        et_value.setBackgroundColor(Color.parseColor("#ff8a9089"));
        et_memo.setBackground(null);
//        et_value.setBackground(PanelStyle.createWhitePanelStyle());
        et_memo.setTextColor(Color.BLACK);
        right.addView(et_memo, LayoutParamTool.createParams(5, 10, 5, 10));
    }

    private void triggerEvent(Object value){
        if (listBoxItemChangedListener!=null && parent.isEditable()) {
            listBoxItemChangedListener.onItemChanged(this, value);
        }
    }

    public static void openSysCamera(Activity activity, String tempFile) throws Exception {
        try {
            SystemTool.startPhotograph(activity, tempFile, true, ListBox.CAMERA_RESULT_CODE);
        } catch (Exception e) {
            throw new Exception("classname:ListBoxPicture,method:initView,message:启动相机失败，请检查系统内存是否还有空间或程序权限不足！\n\n错误信息:"+e.getMessage());
        }
    }

    public static void openChooseAlbumDialog(Activity activity) {
        Intent albumIntent = new Intent(Intent.ACTION_PICK);
        albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(albumIntent, ListBox.ALBUM_RESULT_CODE);
    }

    public interface OnCameraButtonListener {
        void onClick(View view, String pictureFile);
    }

    public interface OnOpenfileButtonListener {
        void onClick(View view);
    }
}
